package com.ruoyi.common.imgconflate;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.media.jai.operator.ScaleDescriptor;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * JAI 提供了丰富的图像处理和图形操作功能，包括图像过滤、变换、合成、校正、分析等。
 */
public class PosterQrcodeUtilToJAI {


    public static void main(String[] args) {
//        String inputImg = "D:\\img\\img\\aaa.png";
//        String outputImg = "D:\\img\\img\\abc.png";
//        int width = 300;
//        int height = 500;
//        try {
//            RenderedOp fileload = JAI.create("fileload", inputImg);
//
//            System.out.println("fileload.getWidth() = " + fileload.getWidth());
//
//            RenderedOp renderedOp = ScaleDescriptor.create(fileload, width / (float) fileload.getWidth(), height / (float) fileload.getHeight() ,0.0F, 0.0F,
//                Interpolation.getInstance(Interpolation.INTERP_BICUBIC), null);
//            JAI.create("filestore", renderedOp, outputImg, "png");
//            System.out.println("图片已处理");
//
//        } catch (Exception e) {
//            System.out.println("图片处理失败");
//            System.out.println("e = " + e.getMessage());
//        }
        overlapImage(null,null,null);
    }

    public static MultipartFile overlapImage(String qrCode, String backgroundPath, Map params) {
        String inputImg = "D:\\img\\img\\aaa.jpg";
        String outputImg = "D:\\img\\img\\abc.png";
        // 加载基础图片
        RenderedOp baseImg = JAI.create("fileload", inputImg);
        // 调用之前的方法获取文字图层
        RenderedOp textLayer = createTextImage("微信昵称", 300, 500);

        // 定义复合模式为源覆盖目标（即新加入的内容会显示在最上层）
        ParameterBlock pb = new ParameterBlock();
        pb.addSource(baseImg).addSource(textLayer);

        // 执行融合操作并将结果保存至磁盘
        RenderedOp result = JAI.create("composite", pb);
        JAI.create("filestore", result, outputImg, "PNG");

        return null;
    }

    private static RenderedOp createTextImage(String text, int width, int height){
        // 创建一个透明背景的缓冲区图像
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = img.createGraphics();
        try{
            Font font = new Font("Arial", Font.BOLD, 72);
            g2d.setFont(font);
            // 设置抗锯齿提高质量
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            // 绘制白色填充矩形作为背景(可选)
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, width, height);
            // 添加黑色文字到中心位置
            g2d.setColor(Color.BLACK);
            FontMetrics fm = g2d.getFontMetrics();
            int x = (width - fm.stringWidth(text)) / 2;
            int y = ((height - fm.getHeight()) / 2) + fm.getAscent();
            g2d.drawString(text, x, y);
        }finally{
            g2d.dispose();
        }
        return JAI.create("image", img);
    }

    public static MultipartFile fileCase(BufferedImage image) {
        //得到BufferedImage对象
        MultipartFile multipartFile = null;
        try {
            //创建一个ByteArrayOutputStream
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            //把BufferedImage写入ByteArrayOutputStream
            ImageIO.write(image, "png", os);
            //ByteArrayOutputStream转成InputStream
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            //InputStream转成MultipartFile
            multipartFile = new MockMultipartFile("file", "file.png", "text/plain", input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return multipartFile;
    }
}
